package com.xuecheng.system.controller;

import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.AuthInfo;
import com.xuecheng.commons.model.vo.CompanyVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.commons.utils.JwtUtils;
import com.xuecheng.system.domain.Company;
import com.xuecheng.web.exception.BusinessException;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xuecheng.system.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * <p>
 * 教育机构前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService  companyService;

//    @GetMapping("/mine")
//    public ResponseResult mine(@RequestHeader("Authorization") String token){
//        //验证token判断用户是否登录
//        Boolean verifyToken = JwtUtils.verifyToken(token);
//        if (!verifyToken) {
//            throw new BusinessException(ErrorCode.NOTFOUND);
//        }
//        //解析token 获取其中的companyId
//        Claims claims = JwtUtils.parserToken(token).getBody();
//        Long companyId = claims.get("companyId", Long.class);
//        //调用service根据id查询机构
//        Company company = companyService.getById(companyId);
//        //构造返回
//        CompanyVo vo = BeanHelper.copyProperties(company, CompanyVo.class);
//        return ResponseResult.okResult(vo);
//    }
    @GetMapping("/mine")
    public ResponseResult mine(){
        AuthInfo info =  AuthInfoHolder.getAuthInfo();
        //调用service根据id查询机构
        Company company = companyService.getById(info.getCompanyId());
        //构造返回
        CompanyVo vo = BeanHelper.copyProperties(company, CompanyVo.class);
        return ResponseResult.okResult(vo);
    }
}
